package com.scpii.api.common.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.scpii.api.domain.common.FieldMask;
import com.scpii.api.util.JsonUtil;

public class FieldMaskTypeHandler extends BaseTypeHandler<FieldMask> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			FieldMask parameter, JdbcType jdbcType) throws SQLException {
		if (parameter == null) {
			ps.setString(i, null);
			return;
		}
		String json = null;
		json = JsonUtil.writeValueAsString(parameter);
		ps.setString(i, json);
	}

	@Override
	public FieldMask getNullableResult(ResultSet rs, String columnName)
			throws SQLException {
		String content = rs.getString(columnName);
		return JsonUtil.readValueWithString(content,
				FieldMask.class);
	}

	@Override
	public FieldMask getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		String content = rs.getString(columnIndex);
		return JsonUtil.readValueWithString(content,
				FieldMask.class);
	}

	@Override
	public FieldMask getNullableResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		String content = cs.getString(columnIndex);
		return JsonUtil.readValueWithString(content,
				FieldMask.class);
	}

}
